package com.lovo.roomProject.room.facility.dao.impl;

import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.pojo.FacilityDO;
import com.lovo.roomProject.room.facility.dao.FacilityDAO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FacilityDAOImpl extends BaseDAO implements FacilityDAO {
    @Override
    public CutPageBean listFacilityPage(int current) {
        this.getConnection();
        String selectSql=" select *";
        String countSql=" select count(*) cnt";
        String fromSql=" from t_facility";
        String limitSql=" limit ?,?";
        CutPageBean cutPageBean=new CutPageBean();
        List list=new ArrayList<>();
        try {
            statement=con.prepareStatement(selectSql+fromSql+limitSql);
            statement.setObject(1,(current-1)*CutPageBean.PAGESIZE);
            statement.setObject(2,CutPageBean.PAGESIZE);
            rs=statement.executeQuery();
            while (rs.next()){
                FacilityDO facilityDO=new FacilityDO();
                facilityDO.setId(rs.getInt("f_id"));
                facilityDO.setDescribe(rs.getString("f_describe"));
                facilityDO.setDate(rs.getDate("f_damage_date"));
                facilityDO.setSolve(rs.getString("f_solve"));
                list.add(facilityDO);
            }
            cutPageBean.setList(list);
            //计算总数的SQL
            statement=con.prepareStatement(countSql+fromSql);
            rs=statement.executeQuery();
            rs.next();
            cutPageBean.setCount(rs.getInt("cnt"));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(rs,statement,con);
        }
        return cutPageBean;
    }

    @Override
    public List listFacility(int id) {
        this.getConnection();
        String sql="select * from t_room r right join t_facility f on f.f_r_id=r.r_id where r.r_id=?";
        List list=new ArrayList();
        try {
            statement=con.prepareStatement(sql);
            statement.setObject(1,id);
            rs=statement.executeQuery();
            while (rs.next()){
                FacilityDO facilityDO=new FacilityDO();
                facilityDO.setId(rs.getInt("f_id"));
                facilityDO.setDescribe(rs.getString("f_describe"));
                facilityDO.setDate(rs.getDate("f_damage_date"));
                facilityDO.setSolve(rs.getString("f_solve"));
                facilityDO.setRoomId(rs.getInt("f_r_id"));
                list.add(facilityDO);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void insert(FacilityDO facilityDO) {
        this.getConnection();
        String sql="insert into t_facility(f_describe,f_damage_date,f_solve,f_r_id) values(?,?,?,?)";
        try {
            statement=con.prepareStatement(sql);
            statement.setObject(1,facilityDO.getDescribe());
            statement.setObject(2,facilityDO.getDate());
            statement.setObject(3,facilityDO.getSolve());
            statement.setObject(4,facilityDO.getRoomId());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close();
        }
    }

    @Override
    public void update(FacilityDO facilityDO) {
        this.getConnection();
        String sql="update t_facility set f_solve=? where f_id=? ";
        try {
            statement=con.prepareStatement(sql);
            statement.setObject(1,facilityDO.getSolve());
            statement.setObject(2,facilityDO.getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
